# -*- coding: utf-8 -*-
import httplib2
import re
from WSThread import WSThread

class DnsFindersBingThread(WSThread):
	def __init__(self, result, ip, pageCounter):
		super().__init__(None, result)
		self.ip = ip
		self.pageCounter = pageCounter
		self.http = httplib2.Http()
	
	def run(self):
		hostRe = re.compile(r"<cite>(\S*)</cite>")
			
		while self.running:
			data = self.http.request("http://www.bing.com/search?q=ip%3a" + self.ip + "&first=" + 
									  str(self.pageCounter.get()*10))
			links = hostRe.findall(str(data[1]))
			if len(links):
				for link in links:
					self.result.put(link if link.find('/') == -1 else link[0:link.find('/')])
			if len(links) < 10:
				self.running = False